package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.R;
import com.mexico.inloancash.bean.BaseBean;
import com.mexico.inloancash.bean.India_CardNumberInfo_ListBean;
import com.mexico.inloancash.bean.India_NewBank_ListBean;
import com.mexico.inloancash.dialog.mIndiaChooseDialog;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.india_interfacedai.IndiaChooseAddressInterface;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IndiaBankInfoActivity extends BaseActivity implements IndiaChooseAddressInterface {

    @BindView(R.id.logon_back)
    ImageView logonBack;

    @BindView(R.id.title)
    TextView title;


    private List<String> mIndiaBankNameList;
    private List<String> mIndiaBankCode;

    @BindView(R.id.user_name)
    EditText mIndiaUserName;//银行卡用户名

    @BindView(R.id.phone_number)
    EditText mIndiaPhoneNumber;//手机号

    @BindView(R.id.bank_card)
    EditText mIndiaBankCard;//银行卡号

    @BindView(R.id.bank_code_ifsc)
    EditText mIndiaEditTextIFSCCode;//银行卡号

    @BindView(R.id.bank_name)
    TextView mIndiaBankName;

    private String mIndiaOrderno = "";
    private String mIndiaOldBankCode = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_view_banking);
        ButterKnife.bind(this);
        title.setText("My Bank Card");
        mIndiaBankNameList = new ArrayList<>();
        mIndiaBankCode = new ArrayList<>();
        Intent intent=this.getIntent();
        if(null!=intent){
            mIndiaOrderno =intent.getStringExtra("orderno");
            if(!TextUtils.isEmpty(mIndiaOrderno)){
                mIndiaGetBankListInfo();
            }
        }
        mIndiaGetBankInfo();
    }

    public void mIndiaGetBankInfo() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> saveBank = api.getBankInfo();
        saveBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    India_NewBank_ListBean baseBean = new Gson().fromJson(response.body(), India_NewBank_ListBean.class);
                    if (Integer.parseInt(baseBean.getCode()) == 200) {
                        List<India_NewBank_ListBean.DataBean> dataBeanList = baseBean.getData();
                        if (dataBeanList.size() > 0) {
                            mIndiaBankNameList.clear();
                            mIndiaBankCode.clear();
                            for (int i = 0; i < dataBeanList.size(); i++) {
                                India_NewBank_ListBean.DataBean bean = dataBeanList.get(i);
                                mIndiaBankNameList.add(bean.getSerpay());
                                mIndiaBankCode.add(bean.getCode());
                            }
                            if (!TextUtils.isEmpty(mIndiaOldBankCode)) {
                                mIndiaBankName.setText(mIndiaBankNameList.get(mIndiaBankCode.indexOf(mIndiaOldBankCode)));
                            }
                        }
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }

    public void mIndiaGetBankListInfo() {
        Log.d("loan", "getBankList");
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> getBank = api.getBankCardInfo(
                MyPreferencesManager.getString("token", ""),
                MyApplication.source,
                mIndiaOrderno);
        getBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    Log.e("data", "getBankList onResponse:" + response.body());
                    if (response.isSuccessful()) {
                        India_CardNumberInfo_ListBean bean = new Gson().fromJson(response.body(), India_CardNumberInfo_ListBean.class);
                        mIndiaBankCard.setText(bean.getData().getCard_no());
                        if (mIndiaBankNameList.size() > 0) {
                            mIndiaBankName.setText(mIndiaBankNameList.get(mIndiaBankCode.indexOf(bean.getData().getBank_code().trim())) + "");
                        }
                        mIndiaOldBankCode = bean.getData().getBank_code().trim();
                        mIndiaEditTextIFSCCode.setText(bean.getData().getBank_ifsc().trim());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            }
        });
    }

    private mIndiaChooseDialog dialog;

    @OnClick({R.id.logon_back, R.id.bank_linear, R.id.commit_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.bank_linear:
                dialog = new mIndiaChooseDialog(this, this, "Bank Card", mIndiaBankNameList, "bank");
                dialog.show();
                break;
            case R.id.commit_button:
                String bankName_ = mIndiaBankName.getText().toString().trim();
                if (TextUtils.isEmpty(bankName_)) {
                    Toast.makeText(IndiaBankInfoActivity.this, "Select the name of the receiving bank!", Toast.LENGTH_SHORT).show();
                    return;
                }

                String cardNumber = mIndiaBankCard.getText().toString().trim();
                if (TextUtils.isEmpty(cardNumber)) {
                    MyToast.show(this, "Please enter your bank card number!");
                    return;
                }

                String IFSC = mIndiaEditTextIFSCCode.getText().toString().trim();
                if (TextUtils.isEmpty(IFSC)) {
                    MyToast.show(this, "Please enter your IFSC!");
                    return;
                }

                mIndiaSaveBank();
                break;
        }
    }

    public void mIndiaSaveBank() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> saveBank = api.saveBank_(
                MyPreferencesManager.getString("token", ""),
                MyApplication.source,
                mIndiaUserName.getText().toString().trim(),
                mIndiaBankCard.getText().toString().trim(),
                mIndiaPhoneNumber.getText().toString().trim(),
                mIndiaBankCode.get(mIndiaBankNameList.indexOf(mIndiaBankName.getText().toString().trim())),
                mIndiaOrderno,
                mIndiaEditTextIFSCCode.getText().toString().trim()
        );
        saveBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                    if (baseBean.getCode() == 200) {
                        MyToast.show(IndiaBankInfoActivity.this, "Modified successfully");
                        //setResult(0);
                        finish();
                    } else if (baseBean.getCode() == 401) {
                        MyPreferencesManager.putString("myPhone", "");
                        MyPreferencesManager.putString("token", "");
                        MyPreferencesManager.putLogin(false);
                        Intent intent = new Intent(IndiaBankInfoActivity.this, IndiaLoginActivity.class);
                        startActivity(intent);
                    } else if (baseBean.getCode() == 60) {

                    } else {
                        MyToast.show(IndiaBankInfoActivity.this, baseBean.getMsg());
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }

    @Override
    public void choose(String type, String string) {
        if (type.equals("bank")) {
            mIndiaBankName.setText(string);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            // getBankList();
        }
    }
}
